<template>
  <div :class="{'show':show}" class="header-search">
    <svg-icon class-name="search-icon" icon-class="search" @click.stop="click"/>
    <el-select ref="headerSearchSelect" v-model="search" :remote-method="querySearch" filterable
               default-first-option
               remote
               placeholder="Search"
               class="header-search-select"
               @change="change">
      <el-option v-for="item in options" :key="item.path" :value="item" :label="item.title.join(' > ')"/>
    </el-select>
  </div>
</template>

<script>
// import Fuse from 'fuse.js'
// import path from 'path'

export default {
  name: 'HeaderSearch',
  data () {
    return {
      show: false,
      search: '',
      options: [],
      fuse: undefined,
      searchPool: []
    }
  },
  watch: {
    routes () {
      this.searchPool = this.generateRoutes(this.routes)
    }
  },
  computed: {
    routes () {
      return this.$store.state.permission.routes
    }
  },
  methods: {
    click () {
      this.show = !this.show
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
      }
    },
    querySearch (query) {
      if (query !== '') {
        this.options = this.fuse.search(query)
      } else {
        this.options = []
      }
    },
    initFuse () {

    },
    generateRoutes (routes, basePath = '/', prefixTitle = []) {

    },
    change () {
    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 18px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

    /deep/ .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}
</style>
